package ggze.GGZEServer.home.controller;

import ggze.GGZEServer.home.dao.inter.SoIndentDao;
import ggze.GGZEServer.home.entity.SoIndent;
import ggze.GGZEServer.home.service.InIndentService;
import ggze.tool.ReturnBody;
import ggze.actuator.ActuatorController;
import ggze.tool.TimeUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ggze.tool.BeanToJson;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import ggze.tool.ReturnTable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

@RestController
@RequestMapping("SoIndentController")
public class SoIndentController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SoIndentDao SoIndentDaoName;
    @Autowired
    private ActuatorController actuatorController;
    @Autowired
    private InIndentService inIndentService;

    @RequestMapping(value = "add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ReturnBody add(@RequestBody JSONObject jsonObject) throws UnsupportedEncodingException {
        SoIndent entity = (SoIndent) BeanToJson.jsonTobean(jsonObject.toString(), SoIndent.class);
        return inIndentService.addInIndent(entity,"0");
    }

    @RequestMapping(value = "deleted", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String deleted(@RequestBody JSONObject jsonObject) throws UnsupportedEncodingException {
        SoIndent entity = (SoIndent) BeanToJson.jsonTobean(jsonObject.toString(), SoIndent.class);
        ReturnBody returnBody = new ReturnBody();
        try {
            SoIndentDaoName.delete("uuid", entity.getUuid());
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }

    @RequestMapping(value = "update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String update(@RequestBody JSONObject jsonObject) throws UnsupportedEncodingException {
        SoIndent entity = (SoIndent) BeanToJson.jsonTobean(jsonObject.toString(), SoIndent.class);
        ReturnBody returnBody = new ReturnBody();
        try {
            String time = TimeUtils.getTimeFormat();
            entity.setF_METHOD_TIME(time);
            actuatorController.execute(entity, "update", SoIndentDaoName);
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }

    @RequestMapping(value = "get", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String get(@RequestBody JSONObject jsonObject) throws UnsupportedEncodingException {
        ReturnBody returnBody = new ReturnBody();
        try {
            int begin = Integer.parseInt(jsonObject.getString("begin"));
            int end = Integer.parseInt(jsonObject.getString("end"));
            String zdname = jsonObject.getString("zdname");
            String zdval = jsonObject.getString("zdval");
            if ("1".equals(zdval)) {
                List<SoIndent> entityLIST = SoIndentDaoName.paging(zdname, zdval, begin, end);
                returnBody.setData(BeanToJson.toJsonArrayString(entityLIST));
            } else {
                List<SoIndent> entityLIST = SoIndentDaoName.customs("select * from SoIndent where " + zdname + "  like '%" + zdval + "%'");
                returnBody.setData(BeanToJson.toJsonArrayString(entityLIST));
            }
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }

    @RequestMapping(value = "num", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String num() throws UnsupportedEncodingException {
        return String.valueOf(SoIndentDaoName.getNum("1", "1"));
    }

    @RequestMapping(value = "seach", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String seach() {
        ReturnTable returnTable = new ReturnTable();
        returnTable.setCount(String.valueOf(SoIndentDaoName.getNum("1", "1")));
        try {
            int begin = Integer.parseInt(request.getParameter("page"));
            int end = Integer.parseInt(request.getParameter("limit"));
            String where = request.getParameter("where");
            begin = (begin - 1) * end;
            List<SoIndent> entityLIST = SoIndentDaoName.customs("select * from SoIndent where" + where + " order by uuid limit " + begin + "," + end + "");
            returnTable.setData(BeanToJson.toJsonArrayString(entityLIST));
        } catch (Exception e) {
            returnTable.setCode("1");
            returnTable.setMsg("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnTable);
    }

    @RequestMapping(value = "getById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String getById(@RequestBody JSONObject jsonObject) {
        ReturnBody returnBody = new ReturnBody();
        try {
            String uuid = jsonObject.getString("uuid");
            SoIndent entity = SoIndentDaoName.isSelect("uuid", uuid);
            returnBody.setData(BeanToJson.toJsonArrayString(entity));
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }
}
